package com.work.straighttalk.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.rockerhieu.emojicon.EmojiconTextView;
import com.work.straighttalk.R;
import com.work.straighttalk.bean.StraightTalkMessage;

import butterknife.Bind;

/**
 * Created by dllo on 16/3/4.
 */
public class ChatSendViewHolder extends BaseViewHolder<StraightTalkMessage> {

    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.iv_avatar)
    SimpleDraweeView ivAvatar;
    @Bind(R.id.tv_message)
    EmojiconTextView tvMessage;
    @Bind(R.id.iv_fail_resend)
    ImageView ivFailResend;
    @Bind(R.id.progress_load)
    ProgressBar progressLoad;
    private OnRecyclerViewClickListener listener;

    public ChatSendViewHolder(Context context, ViewGroup root, OnRecyclerViewClickListener listener) {
        super(context, root, R.layout.item_chat_send);
        this.listener = listener;
    }


    @Override
    public void bindData(StraightTalkMessage message) {
        EMMessage emMessage = message.getEmMessage();
        emMessage.setMessageStatusCallback(new EMCallBack() {
            @Override
            public void onSuccess() {
                ((Activity) getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressLoad.setVisibility(View.INVISIBLE);
                        ivFailResend.setVisibility(View.INVISIBLE);
                    }
                });
            }

            @Override
            public void onError(int i, String s) {
                ((Activity) getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ivFailResend.setVisibility(View.VISIBLE);
                        progressLoad.setVisibility(View.GONE);
                    }
                });
            }

            @Override
            public void onProgress(int i, String s) {
                ((Activity) getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressLoad.setVisibility(View.VISIBLE);
                        ivFailResend.setVisibility(View.GONE);
                    }
                });
            }
        });
        EMTextMessageBody body = (EMTextMessageBody) emMessage.getBody();
        tvMessage.setText(body.getMessage());
        switch (emMessage.status()) {
            case FAIL:

                ivFailResend.setVisibility(View.VISIBLE);
                progressLoad.setVisibility(View.GONE);
                break;
            case SUCCESS:
                progressLoad.setVisibility(View.INVISIBLE);
                break;
            case INPROGRESS:
                progressLoad.setVisibility(View.VISIBLE);
                ivFailResend.setVisibility(View.GONE);
                break;
            case CREATE:
                progressLoad.setVisibility(View.VISIBLE);
                ivFailResend.setVisibility(View.GONE);
                break;
        }
    }
}
